1   /*
2    * Copyright (C) 2010 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static com.google.common.collect.DiscreteDomain.integers;
20  
21  import com.google.caliper.BeforeExperiment;
22  import com.google.caliper.Benchmark;
23  import com.google.caliper.Param;
24  
25  import java.util.Set;
26  
27  /**
28   * Very simple powerSet iteration benchmark.
29   *
30   * @author Kevin Bourrillion
31   */
32  public class PowerSetBenchmark {
33    @Param({"2", "4", "8", "16"}) int elements;
34  
35    Set<Set<Integer>> powerSet;
36  
37    @BeforeExperiment void setUp() {
38      Set<Integer> set = ContiguousSet.create(Range.closed(1, elements), integers());
39      powerSet = Sets.powerSet(set);
40    }
41  
42    @Benchmark int iteration(int reps) {
43      int sum = 0;
44      for (int i = 0; i < reps; i++) {
45        for (Set<Integer> subset : powerSet) {
46          for (Integer value : subset) {
47            sum += value;
48          }
49        }
50      }
51      return sum;
52    }
53  }